<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>从双倍数组中还原原数组</title>
</head>
<body>
  <script>
    function fn(changed) {
      // 将 changed.length 和 1 转换为二进制，数字进行相等，相等的为1（0相等为0），不相等的为0，结果再转为十进制
      // const a = 5;        // 00000000000000000000000000000101
      // const b = 3;        // 00000000000000000000000000000011
      // console.log(a & b); // 00000000000000000000000000000001 // 1
      if (changed.length & 1) return [];
      changed.sort((a, b) => a - b);
      const map = {}, ans = [];
      for (const num of changed) map[num] = (map[num] || 0) + 1;
      for (const num of changed) {
        if (!map[num]) continue;
        if (!map[num * 2]) return [];
        map[num]--
        map[num * 2]--
        ans.push(num)
      }
      return ans
    }
    console.log(fn([1, 3, 4, 2, 6, 8]))
  </script>
</body>
</html>